﻿using Mayn.Video;
using Mayn.Video.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mayn.Video
{
    static class Program
    {

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Upgrade();
            //Application.Run(new FrmCamera());
        }

        //升级
        static void Upgrade()
        {
            string tempPath = Path.GetTempPath();
            string upgradeConfigFileName = "UpgradeList.xml";
            string localXmlPath = string.Format("{0}\\{1}", Application.StartupPath, upgradeConfigFileName);

            ReleaseList localRelease = new ReleaseList(localXmlPath);

            try
            {
                //下载服务器上的upgradeList文件
                DownloadTool.DownloadFile(tempPath, localRelease.ReleaseUrl, upgradeConfigFileName);
            }
            catch (Exception)
            {
                if (File.Exists(tempPath + upgradeConfigFileName))
                {
                    File.Delete(tempPath + upgradeConfigFileName);
                }
                MessageBox.Show("下载更新文件失败，请检查网络和文件夹权限");
                Application.Exit();
                AppTool.Start(localRelease.ApplicationStart);
                return;
            }

            var remoteRelease = new ReleaseList(tempPath + upgradeConfigFileName);
            if (localRelease.Compare(remoteRelease) != 0)
            {
                string fileName = string.Format("{0}\\{1}", Application.StartupPath, "Download.exe");
                System.Diagnostics.Process.Start(fileName, "true");
                Application.Exit();
                return;
            }
        }
    }
}
